﻿/*
Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
For licensing, see LICENSE.html or http://ckeditor.com/license
*/

(function()
{
	// Map 'true' and 'false' values to match W3C's specifications
	// http://www.w3.org/TR/REC-html40/present/frames.html#h-16.5	
	var ATTRTYPE_OBJECT = 1,ATTRTYPE_PARAM = 2;

	var attributesMap =
	{
		id : [ { type : ATTRTYPE_OBJECT, name :  'id' } ],
		align : [ { type : ATTRTYPE_OBJECT, name : 'align' }],
		'class' : [ { type : ATTRTYPE_OBJECT, name : 'class' }],
		width : [ { type : ATTRTYPE_OBJECT, name : 'width' }],
		height : [ { type : ATTRTYPE_OBJECT, name : 'height' }],
		style : [{ type : ATTRTYPE_OBJECT, name : 'style' } ]
	};
	var names = ['title','plain','fit','border','scrollIncrement','scrollDuration'];
	for ( var i = 0 ; i < names.length ; i++ )
		attributesMap[ names[i] ] = [ { type : ATTRTYPE_OBJECT, name : names[i] }];
		
	names = [ 'onLoad', 'onSelect', 'onClose', 'onAdd','onUpdate' ,'onContextMenu','tools'];	
	for ( var i = 0 ; i < names.length ; i++ )	
		attributesMap[ names[i] ] = [ { type : ATTRTYPE_PARAM, name : names[i] }];
		
	names = [ 'border', 'plain' ];
	for ( i = 0 ; i < names.length ; i++ )
		attributesMap[ names[i] ][0]['default']= false;
    //console.info(attributesMap);
	
	function loadValue( spcNode ,paramMap )
	{
		var attributes = attributesMap[ this.id ];
		if ( !attributes )
			return;	
		var isCheckbox = this instanceof CKEDITOR.ui.dialog.checkbox;
		for ( var i = 0 ; i < attributes.length ; i++ )
		{
			var attrDef = attributes[ i ];
			switch ( attrDef.type )
			{
				case ATTRTYPE_OBJECT:
					if ( !spcNode )
						continue;										
					if ( spcNode.getAttribute( attrDef.name ) !== null )
					{
						//attrDef.name
						var value = spcNode.getAttribute( attrDef.name );
						
						if ( isCheckbox )
							this.setValue( value.toLowerCase() == 'true' );
						else
							this.setValue( value );
						return;
					}
					else if ( isCheckbox )
						this.setValue( !!attrDef[ 'default' ] );
					break;
				case ATTRTYPE_PARAM:
					if ( !spcNode )
						continue;
					if ( attrDef.name in paramMap )
					{
						value = paramMap[ attrDef.name ];
						if ( isCheckbox )
							this.setValue( value.toLowerCase() == 'true' );
						else
							this.setValue( value );
						return;
					}
					else if ( isCheckbox )
						this.setValue( !!attrDef[ 'default' ] );
			}
		}
	}

	function commitValue( spcNode ,paramMap )
	{
		var attributes = attributesMap[ this.id ];
		if ( !attributes )
			return;
		
		var isRemove = ( this.getValue() === '' ),
			isCheckbox = ( this instanceof CKEDITOR.ui.dialog.checkbox );

		for ( var i = 0 ; i < attributes.length ; i++ )
		{
			var attrDef = attributes[i];
			switch ( attrDef.type )
			{
				case ATTRTYPE_OBJECT:
					// Avoid applying the data attribute when not needed (#7733)
					if ( !spcNode || ( attrDef.name == 'data' && !spcNode.hasAttribute( 'data' ) ) )
						continue;
					var value = this.getValue();
					
					if ( isRemove || isCheckbox && value === attrDef[ 'default' ] )
						spcNode.removeAttribute( attrDef.name );
					else
						spcNode.setAttribute( attrDef.name, value );
					break;
				case ATTRTYPE_PARAM:
					if ( !spcNode )
						continue;
					value = this.getValue();
					
					if ( isRemove || isCheckbox && value === attrDef[ 'default' ] )
					{
						if ( attrDef.name in paramMap )
							paramMap[ attrDef.name ].remove();
					}
					else
					{
						if ( attrDef.name in paramMap )
							paramMap[ attrDef.name ].setAttribute( 'value', value );
						else
						{
								
							var param = new CKEDITOR.dom.element( 'param' );
							//var param = CKEDITOR.dom.element.createFromHtml( '<cke:param></cke:param>', spcNode.getDocument() );
							param.setAttributes( { name : attrDef.name, value : value } );
							if ( spcNode.getChildCount() < 1 ){
								param.appendTo( spcNode );}
							else
								param.insertBefore( spcNode.getFirst() );
						}
					}
			}
		}
	}

	CKEDITOR.dialog.add( 'stab', function( editor )
	{
		var iframeLang = editor.lang.iframe,spcLang = editor.lang.spc,
			commonLang = editor.lang.common,
			dialogadvtab = editor.plugins.dialogadvtab;
		return {
			title : spcLang.stab.title,
			minWidth : 350,
			minHeight : 260,
			onShow : function()
			{
				// Clear previously saved elements.
				this.fakeImage = this.spcNode = null;
				var fakeImage = this.getSelectedElement();				
				if ( fakeImage && fakeImage.data( 'cke-real-element-type' ) && fakeImage.data( 'cke-real-element-type' ) == 'stab' )
				{
					this.fakeImage = fakeImage;					
					var realElement = editor.restoreRealElement( fakeImage ),
					spcNode = null, paramMap = {};					
					if ( realElement.getName() == 'spcobject' )
					{
						spcNode = realElement;
						var paramList = spcNode.getElementsByTag( 'param' );
						for ( var i = 0, length = paramList.count() ; i < length ; i++ )
						{
							var item = paramList.getItem( i ),
								name = item.getAttribute( 'name' ),
								value = item.getAttribute( 'value' );
							paramMap[ name ] = value;
						}
					}
					this.spcNode = spcNode;
					this.setupContent( spcNode, paramMap, fakeImage );
				}
			},
			onOk : function()
			{
				var spcNode;
				if ( !this.fakeImage )
					spcNode = new CKEDITOR.dom.element( 'spcobject' );
				    //spcNode = CKEDITOR.dom.element.createFromHtml( '<cke:spcobject></cke:spcobject>', editor.document );
				else
					spcNode = this.spcNode;				
				if ( spcNode )
				{
					paramMap = {};
					var paramList = spcNode.getElementsByTag( 'param' );
					for ( var i = 0, length = paramList.count() ; i < length ; i++ )
						paramMap[ paramList.getItem( i ).getAttribute( 'name' ) ] = paramList.getItem( i );					
				}
				
				// A subset of the specified attributes/styles
				// should also be applied on the fake element to
				// have better visual effect. (#5240)
				//console.info(paramMap);
				var extraStyles = {}, extraAttributes = {};
				this.commitContent( spcNode, paramMap, extraStyles, extraAttributes );
				// Refresh the fake image.
				var newFakeImage = editor.createFakeElement( spcNode, 'cke_stab', 'stab', true );
				newFakeImage.setAttributes( extraAttributes );
				newFakeImage.setStyles( extraStyles );
				if ( this.fakeImage )
				{
					newFakeImage.replace( this.fakeImage );
					editor.getSelection().selectElement( newFakeImage );
				}
				else
					editor.insertElement( newFakeImage );
			},
			contents : [
				{
					id : 'info',
					label : commonLang.generalTab,
					accessKey : 'I',
					elements :
					[
						{
							type : 'vbox',
							children :
							[
								{
									id : 'title',
									type : 'text',
									width: "80%",
									label : 'title',
									//required : true,
									setup : loadValue,
									commit : commitValue
								}
							]
						},{
							type : 'hbox',
							children :	
							[
								{
									id : 'width',
									type : 'text',
									label : 'width',
									style : 'width:95px',
									validate : CKEDITOR.dialog.validate.htmlLength( editor.lang.common.invalidHtmlLength.replace( '%1', editor.lang.common.width ) ),
									setup : loadValue,									
									commit : commitValue
								},{
									id : 'height',
									type : 'text',
									label : 'height',
									style : 'width:95px',
									validate : CKEDITOR.dialog.validate.htmlLength( editor.lang.common.invalidHtmlLength.replace( '%1', editor.lang.common.height ) ),
									setup : loadValue,
									commit : commitValue
								},{
									id : 'scrollIncrement',
									type : 'text',
									label : 'scrollIncrement',
									style : 'width:95px',
									validate : CKEDITOR.dialog.validate.integer( "must integer" ),
									setup : loadValue,
									commit : commitValue
								},{
									id : 'scrollDuration',
									type : 'text',
									label : 'scrollDuration',
									style : 'width:95px',
									validate : CKEDITOR.dialog.validate.integer( "must integer"),
									setup : loadValue,
									commit : commitValue
								}
							]							
						},{
							type : 'hbox',
							children :	
							[
								{
									id : 'plain',
									type : 'checkbox',
									label : 'plain',
									setup : loadValue,
									commit : commitValue
								},{
									id : 'fit',
									type : 'checkbox',
									label : 'fit',
									setup : loadValue,
									commit : commitValue
								},{
									id : 'border',
									type : 'checkbox',
									label : 'border',
									setup : loadValue,
									commit : commitValue
								}
							]						
							
						},{
							type : 'vbox',
							children :
							[
								{
									id : 'tools',
									type : 'textarea',
									label : 'tools',
									//required : true,
									//validate : CKEDITOR.dialog.validate.notEmpty( iframeLang.noUrl ),
									setup : loadValue,
									commit : commitValue
								}
							]
						}
					]
				},
				dialogadvtab && dialogadvtab.createAdvancedTab( editor)
			]
		};
	});
})();
